package com.hourses.service.impl;

import com.hourses.mapper.RoleAuthMapper;
import com.hourses.mapper.RoleMapper;
import com.hourses.mapper.UserRoleMapper;
import com.hourses.page.Page;
import com.hourses.pojo.Result;
import com.hourses.pojo.Role;
import com.hourses.pojo.RoleAuth;
import com.hourses.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/4/29 14:35
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询数据库所有用户角色的列表(ServiceImpl)
     * @return
     */
    @Override
    public List<Role> selectAllRoles() {
        List<Role> roles = roleMapper.selectAllRoles();
        return roles;
    }

    /**
     * 查询用户已有的角色
     * @param userId
     * @return
     */
    @Override
    public List<Role> selectUserRolesByUserRole(Integer userId) {
        List<Role> roles = roleMapper.selectUserRolesByUserId(userId);
        return roles;
    }


    /**
     * 查询角色列表
     * @param role
     * @param page
     * @return
     */
    @Override
    public Page getRolestPage(Role role, Page page) {
        //step1:查询满足当前条件的总函数
        Integer countRoles = roleMapper.getRoleTotalSize(role);
        //step2:查询满足当前条件的roleList
        List<Role> roleListPage = roleMapper.getRoleListPage(role, page);
        page.setTotalNum(countRoles);
        page.setResultList(roleListPage);
        return page;
    }

    //添加角色
    @Autowired
    private StringRedisTemplate redisTemplate;
//    @CacheEvict("allRoles:")  //先把当前缓存中的所有role清空
    @Override
    public Result addRole(Role role) {
        //先判断要添加的角色是否存在，注意：roleName与roleCode都不能与数据库中的数据相同
        Integer count = roleMapper.selectRoleByNameOrCode(role);
        if (count>0){
            //说明数据库当中已经有现在要添加的role
            return Result.err(Result.CODE_ERR_BUSINESS,"添加失败！角色已经存在...");
        }
        //数据库当中没有想要添加的role
        Integer addCount = roleMapper.addRole(role);
        redisTemplate.delete("allRoles:");
        return Result.ok("添加成功！",addCount);
    }

    //启用或者禁用角色
//    @CacheEvict("allRoles:")  //清缓存，防止在给用户添加角色的时候系统走旧缓存导致数据不一致的情况
    @Override
    public Result startOrCloseRole(Role role) {
        Integer count = roleMapper.startOrCloseRole(role);
        if (count>0){
            //更新成功
            redisTemplate.delete("allRoles:");
            return Result.ok("启用/禁用成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"启用/禁用失败！请联系管理员...");
    }

    //根据roleId删除角色
    @Autowired
    private RoleAuthMapper roleAuthMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
//    @CacheEvict("allRoles:")  //清缓存，防止在给用户添加角色的时候系统走旧缓存导致数据不一致的情况
    @Override
    public Result deleteRoleById(Integer roleId) {
        //删除role表中响应的role
        Integer count = roleMapper.deleteRoleById(roleId);
        if (count>0){
            //删除成功以后删除auth_role表中对应的关系
            roleAuthMapper.deleteByRoleId(roleId);
            //同时删除userRole表中对应的关系
            userRoleMapper.deleteByRoleId(roleId);
            redisTemplate.delete("allRoles:");
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！请联系管理员...");
    }

    //根据roleId查询authIds
    @Override
    public List<Integer> selectAuthIdsByRoleId(Integer roleId) {
        List<Integer> authIdList = roleMapper.selectAuthIdsByRoleId(roleId);
        return authIdList;
    }

    //根据roleId删除对应role的所有auth
    @Override
    public Integer deleteAuthsByRoleId(Integer roleId) {
        Integer count = roleMapper.deleteAuthsByRoleId(roleId);
        return count;
    }

    //向role_auth表中添加数据
    @Override
    public Integer insertRoleAuth(RoleAuth roleAuth) {
        Integer count = roleMapper.insertRoleAuth(roleAuth);
        return count;
    }

    //修改角色描述
//    @CacheEvict("allRoles:")  //清一下缓存，防止下次获取所有角色的时候获取到旧数据
    @Override
    public Result updateRoleDesc(Role role) {
        Integer count = roleMapper.updateRoleDesc(role);
        if (count>0){
            redisTemplate.delete("allRoles:");
            return Result.ok("修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改失败！请联系管理员...");
    }
}
